package com.example.mybatis.mapper;

import com.example.mybatis.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoMapper {

    @ResultMap("resultMap")
    @Select("select * from userinfo")
    List<UserInfo> getUserInfoAll();

    @Select("select * from userinfo where delete_flag = #{deleteFlag}")
    List<UserInfo> gerUserInfoByDeleteFlag(Integer flag);

    @Select("select * from userinfo where delete_flag = #{param1} and gender = #{gender}")
    List<UserInfo> getUserInfoAll2(Integer flag, Integer gender);

    @Select("select * from userinfo where delete_flag = #{deleteFlag} and gender = #{gender}")
    List<UserInfo> getUserInfoAll3(@Param("deleteFlag") Integer flag, Integer gender);

//    @Select("select * from userinfo")
//    List<UserInfo> getUserInfoAll(String id);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into userinfo (username, password, age, gender) values (#{username}, #{password}, #{age}, #{gender})")
    Integer insert(UserInfo userInfo);

    @Insert("insert into userinfo (username, password, age, gender) " +
            " values (#{userInfo.username}, #{userInfo.password}, #{userInfo.age}, #{userInfo.gender})")
    Integer insert2(@Param("userInfo") UserInfo userInfo);

    @Delete("delete from userinfo where id = #{id}")
    Integer delete(Integer id);

    @Update("update userinfo set password = #{password}, age = #{age}, gender = #{gender} where id = #{id}")
    Integer update(UserInfo userInfo);

    @Select("select id, username, password, age, gender, phone, " +
            " delete_flag as deleteFlag, create_time as createTime, " +
            " update_time as updateTime from userinfo;")
    List<UserInfo> selectUserInfos();

    //指定结果映射关系

    @Results(id = "resultMap", value = {
            @Result(column = "delete_flag", property = "deleteFlag"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime")
    })
    @Select("select * from userinfo")
    List<UserInfo> selectUserInfos2();

    @Select("select * from userinfo")
    List<UserInfo> selectUserInfos3();

    @Select("select * from userinfo")
    List<UserInfo> selectAlluser2();

    @Select("select * from userinfo where username = #{usernmae}")
    List<UserInfo> getUserByName(String username);

    @Select("select * from userinfo where username = '${usernmae}'")
    List<UserInfo> getUserByName2(String username);


    @Select("select * from userinfo where username = '${username}' and password = '${password}'")
    UserInfo queryUserByNameAndPassword(String username, String password);

    @Select("select * from userinfo order by id ${order}")
    List<UserInfo> queryUserListByOrder(String order);

    @Select("select * from userinfo where username like concat('%',#{username},'%');")
    List<UserInfo> queryUserListByLike(String username);

    @Insert("<script>" +
            "insert into userinfo " +
            "        <trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\"> " +
            "            <if test=\"username!=null\"> " +
            "                username, " +
            "            </if> " +
            "            <if test=\"password!=null\"> " +
            "                password, " +
            "            </if> " +
            "            <if test=\"age!=null\"> " +
            "                age, " +
            "            </if> " +
            "            <if test=\"gender!=null\"> " +
            "                gender " +
            "            </if> " +
            "        </trim> " +
            "        values " +
            "        <trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\"> " +
            "            <if test=\"username!=null\"> " +
            "                #{username}, " +
            "            </if> " +
            "            <if test=\"password!=null\"> " +
            "                #{password}, " +
            "            </if> " +
            "            <if test=\"age!=null\"> " +
            "                #{age}, " +
            "            </if> " +
            "            <if test=\"gender!=null\"> " +
            "                #{gender}, " +
            "            </if> " +
            "        </trim>" +
            "</script>")
    Integer insertByCondition(UserInfo userInfo);
}
